<?php
/**
 * Created by PhpStorm.
 * User: fire
 * Date: 2018/10/10
 * Time: 17:39
 */
namespace App\Filters;

use Illuminate\Http\Request;

abstract class Filters {
    protected $request,$builder;
    protected $filters=[];

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

   /* public function apply($builder)
    {
        $this->builder = $builder;


        foreach ($this->filters as $filter){
            if(! $this->hasFilter($filter)) return;

            $this->$filter($this->request->$filter);

        }

        return $this->builder;
    }*/

   /* protected function hasFilter($filter)
    {
        return method_exists($this,$filter) && $this->request->has($filter);
    }*/

    public function apply($builder)
    {
        $this->builder = $builder;

        foreach ($this->getFilters() as $filter => $value){
            if(method_exists($this,$filter)){  // 注：此处是 hasFilter() 方法的重构
                $this->$filter($value);
            }
        }

        return $this->builder;
    }

    public function getFilters()
    {
//        dd($this->request->intersect($this->filters));
        return $this->request->intersect($this->filters);
    }

}